package com.cmrx.model.controller;

import com.cmrx.model.entity.CaseDetail;
import com.cmrx.model.entity.User;
import com.cmrx.model.entity.Victim;
import com.cmrx.model.service.InvestigationService;
import com.cmrx.model.service.ScenePictureService;
import com.cmrx.model.service.SysdictService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.vo.IRespCode;
import com.cmrx.model.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("picture")
@Slf4j
@Api(value = "/picture",description = "方位图相关接口类")
public class ScenePictureController {

    @Autowired
    private ScenePictureService scenePictureService;
    @Autowired
    private InvestigationService investigationService;
    @Autowired
    private SysdictService sysdictService;

    @RequestMapping(value = "/add/picture",method = RequestMethod.POST)
    @ApiOperation(value = "添加方位图接口")
    public ResultVo addPictuer(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.error(IRespCode.user_ERR_CODE,"token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.error(IRespCode.user_ERR_CODE,"用户信息为空");
        }
        if(StringUtils.isEmpty(map.get("investigationid"))){
            return ResultVoUtil.error(IRespCode.user_ERR_CODE,"investigationid为空");
        }
        if(StringUtils.isEmpty(map.get("reserver4"))||StringUtils.isEmpty(map.get("reserver6"))){
            return ResultVoUtil.error(IRespCode.SERVICE_ERR_CODE,"经度或维度为空");
        }else {
            String x = map.get("reserver4");
            String y = map.get("reserver6");
            double dx = Double.valueOf(x);
            double dy = Double.valueOf(y);
            DecimalFormat df = new DecimalFormat("#,######.0000");
            map.put("reserver4",df.format(dx));
            map.put("reserver6",df.format(dy));
        }
        map.put("userid",user.getUserid());
        return ResultVoUtil.success(scenePictureService.addPictuer(map,user));
    }

    @RequestMapping(value = "/select/picture",method = RequestMethod.POST)
    @ApiOperation(value = "方位图列表接口")
    public ResultVo selectPictuer(@RequestBody Map<String,String> map) {
        if(StringUtils.isEmpty(map.get("investigationid"))){
            return ResultVoUtil.message("investigationid为空");
        }
        return ResultVoUtil.success(scenePictureService.selectPictuer(map));
    }

    @RequestMapping(value = "/delete/picture",method = RequestMethod.POST)
    @ApiOperation(value = "删除方位图接口")
    public ResultVo deletePictuer(@RequestBody Map<String,String> map) {
        String id = map.get("id");
        String token = "";
        //token信息
        if(map.containsKey("token")){
            token = map.get("token");
        }
        if(token==null||"".equals(token)){
            return ResultVoUtil.message("用户信息为空");
        }
        User user = JwtUtil.deToken(token);
        if(StringUtils.isEmpty(map.get("id"))){
            return ResultVoUtil.message("id为空");
        }
        scenePictureService.deletePictuer(map);
        return ResultVoUtil.success(null);
    }

    @RequestMapping(value = "/select/pictureinfo",method = RequestMethod.POST)
    @ApiOperation(value = "方位图编辑信息查询接口")
    public ResultVo selectPictureinfo(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.message("token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.message("用户信息为空");
        }
        String investigationid = map.get("investigationid");
        if(StringUtils.isEmpty(investigationid)){
            return ResultVoUtil.message("investigationid为空");
        }
        //接勘时间+勘验地点+被害人+案件类别+现场方位示意图
        Map<String,Object> result = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String time = sFormat.format(date);

        List<CaseDetail> caseDetails =  investigationService.selectListLawCase(null, investigationid);
        CaseDetail caseDetail = caseDetails.get(0);

        String received_date = caseDetail.getReceived_date() == null?"":caseDetail.getReceived_date();
        String investigation_place =  caseDetail.getInvestigation_place()== null?"":caseDetail.getInvestigation_place();
        List<Victim> victims =  caseDetail.getVictimsList();
        String victimstring = "";
        if(!victims.isEmpty()&&victims.size()>0){
            for (Victim victim:victims) {
                victimstring = victimstring + victim.getName()+"、";
            }
        }
        String case_type_name=  caseDetail.getCase_type_name()==null?"":caseDetail.getCase_type_name();
        String title = received_date + investigation_place + victimstring + case_type_name + "现场方位示意图";
        result.put("title",title);
        result.put("occurrence_date_from",caseDetail.getOccurrence_date_from()==null?"":caseDetail.getOccurrence_date_from());
        result.put("scene_detail",caseDetail.getScene_detail()==null?"":caseDetail.getScene_detail());
        result.put("draw_time",time);
        result.put("draw_person",user.getUserName());
        result.put("draw_organize",user.getOrganName());
        result.put("investigationid",investigationid);
        return ResultVoUtil.success(result);
    }


    @RequestMapping(value = "/select/photoinfo",method = RequestMethod.POST)
    public ResultVo selectPhotoinfo(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.message("token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.message("用户信息为空");
        }
        Date date = new Date();
        SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sFormat.format(date);
        Map<String,Object> result = new HashMap<>();
        result.put("createUser",user.getUserName());
        result.put("updateUser",user.getUserName());
        result.put("createDatetime",time);
        result.put("photoTypeList",sysdictService.selectSysdict("XCZPZLDM"));
        return ResultVoUtil.success(result);
    }

    @RequestMapping(value = "/select/photo",method = RequestMethod.POST)
    public ResultVo selectPhoto(@RequestBody Map<String,String> map) {
        String investigationid = map.get("investigationid");
        if(StringUtils.isEmpty(investigationid)){
            return ResultVoUtil.message("investigationid为空");
        }
        return ResultVoUtil.success(scenePictureService.selectPhoto(map));
    }

    @RequestMapping(value = "/delete/photo",method = RequestMethod.POST)
    public ResultVo deletePhoto(@RequestBody Map<String,String> map) {
        if(StringUtils.isEmpty(map.get("id"))){
            return ResultVoUtil.message("id");
        }
        scenePictureService.deletePhoto(map);
        return ResultVoUtil.success(null);
    }

    @RequestMapping(value = "/addorupdate/photo",method = RequestMethod.POST)
    public ResultVo addorupdatePhoto(@RequestBody Map<String,String> map) {
        String token = map.get("token");
        if(StringUtils.isEmpty(token)){
            return ResultVoUtil.message("token为空");
        }
        User user = JwtUtil.deToken(token);
        if(user==null){
            return ResultVoUtil.message("用户信息为空");
        }
        if(map.get("investigationid")==null){
            return ResultVoUtil.message("investigationid为空");
        }
        map.put("userid",user.getUserid());
        return ResultVoUtil.success(scenePictureService.addorupdatePhoto(map,user));
    }
    //查询原图
    @RequestMapping(value = "/original/picture",method = RequestMethod.POST)
    @ApiOperation(value = "原图接口")
    public ResultVo selectOriginalPicture(@RequestBody Map<String,String> map) {
        String id = map.get("id");
        if(StringUtils.isEmpty(id)){
            return ResultVoUtil.message("id为空");
        }
        return ResultVoUtil.success(scenePictureService.selectOriginalPicture(id));
    }












}
